#!/bin/bash

# phone test db master = 172.16.3.111
# phone test shared master = 172.16.3.131


# real game db master = 172.16.1.161
# 172.16.1.181
# 172.16.1.182
# real item shared 01 = 172.16.2.31
# real item shared 02 = 172.16.2.32
# real item shared 03 = 172.16.2.33
# real item shared 04 = 172.16.2.34
# real item shared 05 = 172.16.2.35
# real item shared 06 = 172.16.2.36
# real item shared 07 = 172.16.2.37
# real item shared 08 = 172.16.2.38
# real item shared 09 = 172.16.2.39
# real item shared 10 = 172.16.2.40

BACKUP_SERVER='172.16.3.131'
RESTORE_SERVER="
172.16.3.132
"

#RESTORE_SERVER="
#172.16.1.181
#172.16.1.182
#172.16.2.31
#172.16.2.32
#172.16.2.33
#172.16.2.34
#"

#BACKUP_SERVER='172.16.3.111'

#RESTORE_SERVER="
#172.16.1.161
#"

pass="rpaqlf"
db_backup_path="/home/gamevil/projectz/shell/db_backup"
db_restore_path="/home/gamevil/db_restore"
DATE=`date +%Y%m%d%H`

db_backup()
{
sshpass -p $pass ssh -oStrictHostKeyChecking=no gamevil@$BACKUP_SERVER \
"
mysqldump -uroot -prpdlaqlf pz_user > pz_user_table_$DATE.sql
mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt -uroot -prpdlaqlf pz_user >  pz_user_procedure_$DATE.sql
"
}

sftp_get()
{
sshpass -p $pass sftp gamevil@$BACKUP_SERVER <<EOF
bi
ha
mget pz_user_*_$DATE.sql
exit
EOF
}

sftp_put()
{
sshpass -p $pass ssh -oStrictHostKeyChecking=no gamevil@$1 "echo"
sshpass -p $pass sftp gamevil@$1 <<EOF
bi
ha
mput pz_user_*_$DATE.sql
exit
EOF
}

db_restore()
{
sshpass -p $pass ssh -oStrictHostKeyChecking=no gamevil@$1 \
"
mysql -ugamevil -pvlwlrpaqlf pz_user < pz_user_table_$DATE.sql
mysql -ugamevil -pvlwlrpaqlf pz_user < pz_user_procedure_$DATE.sql
"
}

db_backup
sftp_get

for ip in $RESTORE_SERVER
do
sftp_put $ip
db_restore $ip
done

